package com.uzai.console.service.tb.tbtokenapi.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.common.utils.CommonUtil;
import com.uzai.console.common.ResponseMsg;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.dto.tb.tbtokenapi.TbTokenApiCallBackDto;
import com.uzai.console.entity.Merchant;
import com.uzai.console.entity.TbRidToken;
import com.uzai.console.entity.TbToken;
import com.uzai.console.entity.TbTokenApi;
import com.uzai.console.mapper.MerchantMapper;
import com.uzai.console.mapper.TbRidTokenMapper;
import com.uzai.console.mapper.TbTokenApiMapper;
import com.uzai.console.mapper.TbTokenMapper;
import com.uzai.console.service.tb.tbridpid.TbRidPidService;
import com.uzai.console.service.tb.tbtokenapi.TbTokenApiCallBackService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.URLDecoder;
import java.util.Map;

/**
 * @author liuqi
 * @date 2021年08月31日 20:10
 */
@Service
public class TbTokenApiCallBackServiceImpl implements TbTokenApiCallBackService {

    private Logger logger = LoggerFactory.getLogger(TbTokenApiCallBackServiceImpl.class);

    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private TbTokenApiMapper tbTokenApiMapper;
    @Autowired
    private TbRidTokenMapper tbRidTokenMapper;
    @Autowired
    private TbRidPidService tbRidPidService;

    @Autowired
    private TbTokenMapper tbTokenMapper;

    @Transactional
    @Override
    public void add(TbTokenApi tbTokenApi) {
        Merchant merchant = merchantMapper.selectById(tbTokenApi.getMerId());
        if (merchant==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "没有该运营商");
        }

        //优先判断是否在淘宝联盟了授权过
        TbToken tbToken = tbTokenMapper.selectByUserIdAndTurnLink(tbTokenApi.getUserId(),0);
        if(tbToken == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "授权的联盟账号在淘宝联盟授权列表不存在！");
        }

        if(tbToken.getMerId().longValue() != tbTokenApi.getMerId().longValue()){
            Merchant merchant1 = merchantMapper.selectById(tbToken.getMerId());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, String.format(ResponseMsg.ALIMM_ACCOUNT_EXIST, CommonUtil.mobileFormat(merchant1.getMobile())));
        }

        //在判断该联盟在手动拉取联盟api里是否存在
        TbTokenApi at = tbTokenApiMapper.selectByUserId(tbTokenApi.getUserId());
        //该淘宝联盟账号不存在，则新增加
        if (at == null){
            tbTokenApiMapper.insertSelective(tbTokenApi);
        }else{
            if(at.getType().intValue() != tbTokenApi.getType().intValue()){//验证type是否一样，不一样即淘宝和饿了么共有
                tbTokenApi.setType(2); //2-淘宝和饿了么共有
            }
            logger.info(at.getMerId() + "::" + tbTokenApi.getMerId());
            if (at.getMerId().longValue() != tbTokenApi.getMerId().longValue()){
                Merchant merchant1 = merchantMapper.selectById(at.getMerId());
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, String.format(ResponseMsg.ALIMM_ACCOUNT_EXIST, CommonUtil.mobileFormat(merchant1.getMobile())));
            }
            tbTokenApi.setId(at.getId());
            tbTokenApi.setVersion(at.getVersion());
            int count = tbTokenApiMapper.updateSelective(tbTokenApi);
            if (count != 1){
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.NET_ERROR);
            }
        }
    }

}
